$(document).ready(function() {
    $('.checkCompare').click(function() {
        var totalChecked = $('#carCompare').find(':checked').length;


        if ($(this).is(":checked")) {
            $(this).parent().find('a').addClass('selected');
        } else {
            $(this).parent().find('a').removeClass('selected');
        }

        switch (totalChecked)
        {
            case 1:
                $('#carCompareSubmit').attr('disabled', true);
                break;
            case 2:
                $('#carCompareSubmit').attr('disabled', false);
                break;
            case 3:
                //alert('Puede compara un maximo de 4 vehiculos!');
                $('#carCompare').find('input:checkbox:not(:checked)').each(function() {
                    $(this).attr('disabled', false);
                });
                break;
            case 4:
                $('#carCompare').find('input:checkbox:not(:checked)').each(function() {
                    $(this).attr('disabled', true);
                });
                //$('.checkCompare').attr('disabled', true);
                break;
        }

    });
    $('form').submit(function() {
        /*$(this).find(':checkbox:checked').each(function() {
         alert($(this).val());
         });*/
        $('#results').html('');
        var dataString = $(this).serialize();
        console.log(dataString);
        $.ajax({
            url: window.location.origin + '/site/ajax_compare_call',
            type: 'POST',
            dataType: 'json',
            data: dataString,
            cache: false,
            beforeSend: function() {
                //$('#loader').show();
                //$('#results').html('');
            },
            success: function(output_string) {
                $('#results').html(output_string);
            }
        });
        return false;
    });
    $('#resetButton').click(function() {
        $('#carCompareSubmit').attr('disabled', true);
        $('.checkCompare').attr('disabled', false);
        $('.checkCompare').find('a.selected').removeClass('selected');
    });
    /*START CAR COMPARE*/
    /*var maxCompare = 4;
     var counter = 1;
     $('.checkCompare').click(function() {
     if (counter != maxCompare) {
     var val = [];
     var items = -1;
     $('#idCarCompareValues').html('');
     $(':checkbox:checked').each(function(i) {
     val[i] = $(this).val();
     $(this).addClass('selected');
     $(this).removeClass('checkCompare');
     if (val[i] != 'checkbox') {
     $("#idCarCompareValues").append("<input type='hidden' id='carID' name='carID" + i + "' value='" + val[i] + "'>");
     $('#carCompareSubmit').attr('disabled', false);
     }
     items++;
     });
     $("#idCarCompareValues").append("<input type='hidden' name='counter' id='counter' value='" + items + "'>");
     
     if (!this.checked) {*/
    /*Add class CHECKCOMPARE to checked option and remove class SELECTED from clicked checkbox*/
    /*$(this).addClass('checkCompare');
     $(this).removeClass('selected');
     counter--;
     if (counter == 0) {
     this.checked = true;
     $('#carCompareSubmit').attr('disabled', true);
     alert('Soleccione como m�nimo 2 veh�culos para comparar.');
     counter++;
     } else {
     if (counter == 1) {
     alert('Soleccione como mínimo 2 vehículos para comparar.');
     this.checked = false;
     $('#carCompareSubmit').attr('disabled', true);
     }
     }
     } else {
     counter++;
     }
     } else {
     if (this.checked) {
     alert('Solo 4 vehículos para comparar son permitidos.');*/
    /*Add class CHECKCOMPARE to checked option and remove class SELECTED from clicked checkbox*/
    /*$(this).addClass('checkCompare');
     $(this).removeClass('selected');
     this.checked = false;
     $('.checkCompare').attr('disabled', true);
     } else {
     $(this).addClass('checkCompare');
     $(this).removeClass('selected');
     $('.checkCompare').attr('disabled', false);
     counter--;
     var val = [];
     var items = -1;
     $('#idCarCompareValues').html('');
     $(':checkbox:checked').each(function(i) {
     val[i] = $(this).val();
     if (val[i] != 'checkbox') {
     $("#idCarCompareValues").append("<input type='hidden' id='carID' name='carID" + i + "' value='" + val[i] + "'>");
     $('#carCompareSubmit').attr('disabled', false);
     }
     items++;
     });
     $("#idCarCompareValues").append("<input type='hidden' name='counter' id='counter' value='" + items + "'>");
     }
     }
     });*/
    /*END CAR COMPARE*/
    /*$('#resetButton').click(function() {
     $('#carCompareSubmit').attr('disabled', true);
     $('#idCarCompareValues').html('');
     counter = 1;
     $('.selected').each(function(i) {
     $(this).addClass('checkCompare');
     $(this).removeClass('selected');
     $('.checkCompare').attr('disabled', false);
     });
     });*/
});


